

package com.github.satant.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.github.satant.vo.CacheTree;

public class TreeUtils {

  public TreeUtils() {
  }

  public static List<CacheTree> buildTree(List<CacheTree> trees) {
    List<CacheTree> list = new ArrayList();
    Iterator i$ = trees.iterator();

    while (i$.hasNext()) {
      CacheTree tree = (CacheTree) i$.next();
      if (tree.getParentId().equals("-1")) {
        list.add(tree);
      }

      Iterator it = trees.iterator();

      while (it.hasNext()) {
        CacheTree t = (CacheTree) it.next();
        if (t.getParentId().equals(tree.getId())) {
          if (tree.getNodes() == null) {
            List<CacheTree> myChildrens = new ArrayList();
            myChildrens.add(t);
            tree.setNodes(myChildrens);
          } else {
            tree.getNodes().add(t);
          }
        }
      }
    }

    return list;
  }
}
